﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using NotesListBox;
using System.Collections.ObjectModel;
using System.Data.SqlClient;
using System.Data;

namespace luanvanFinal
{
    /// <summary>
    /// Shows an instance of the NotesListBoxControl in
    /// the AdornerLayer associated with this Window.
    /// The NotesListBoxControl was designed to work with an AdornerLayer.
    /// 
    /// Though it is a stand alone control in its own right, so can be used
    /// as such. But when used as a standalone control, it will have to be part
    /// of the Window/Control layout. Using it in the AdornerLayer conserves the
    /// screen real estate of the Window/Control using the NotesListBoxControl, as
    /// it shown in the layer above these control (the AdornerLayer basically).
    /// 
    /// I have included a small test setup where a Person object has
    /// a collection of Note objects that can be shown and edited using the
    /// NotesListBoxControl
    /// </summary>
    public partial class wdNote : Window
    {
        clsSticky sticky;
        #region Ctor
        public wdNote()
        {
            InitializeComponent();


            #region Wire up Routed Events
            //Wire up the Note Added Event, which will come from the 
            //NotesListBoxControl on the AdornerLayer
            EventManager.RegisterClassHandler(
                typeof(NotesListBoxControl),
                NotesListBoxControl.NoteAddedEvent,
                new NoteEventHandler(
                    (s, ea) =>
                    {
                        Console.WriteLine(CreateNoteMessage(ea.Note));
                    }));


            //Wire up the Note Removed Event, which will come from the 
            //NotesListBoxControl on the AdornerLayer
            EventManager.RegisterClassHandler(
                typeof(NotesListBoxControl),
                NotesListBoxControl.NoteRemovedEvent,
                new NoteEventHandler(
                    (s, ea) =>
                    {
                        Console.WriteLine(CreateNoteMessage(ea.Note));
                    }));

            //Wire up the Note Changed Event, which will come from the 
            //NotesListBoxControl on the AdornerLayer
            EventManager.RegisterClassHandler(
                typeof(NotesListBoxControl),
                NotesListBoxControl.NoteChangedEvent,
                new NoteEventHandler(
                    (s, ea) =>
                    {
                        Console.WriteLine(CreateNoteMessage(ea.Note));
                    }));
            #endregion

            this.Loaded += new RoutedEventHandler(Window1_Loaded);


        }
        #endregion

        #region Private Methods
        private void Window1_Loaded(object sender, RoutedEventArgs e)
        {
            #region Create Dummy Data

            ObservableCollection<Person> people = new ObservableCollection<Person>();

            clsProject project = new clsProject();
            DataTable dt = project.getProjectName();

            for (int i = 0; i < dt.Rows.Count; i++)
            {
                Person p = new Person { PersonId = dt.Rows[i][0].ToString() };
                p.Notes.Add(new Note { DateCreated = DateTime.Now, Data = "sacha note1" });
                people.Add(p);
            }

            /*Person p = new Person { PersonId = "sacha" };
            p.Notes.Add(new Note { DateCreated = DateTime.Now, Data = "sacha note1" });
            p.Notes.Add(new Note { DateCreated = DateTime.Now, Data = "sacha note2" });
            p.Notes.Add(new Note { DateCreated = DateTime.Now, Data = "sacha note3" });
            people.Add(p);


            Person p2 = new Person { PersonId = "bert" };
            p2.Notes.Add(new Note { DateCreated = DateTime.Now, Data = "bert note1" });
            people.Add(p2);

            Person p3 = new Person { PersonId = "ngoc" };
            people.Add(p3);*/

            lstPeople.ItemsSource = people;

            #endregion
        }

        private String CreateNoteMessage(Note note)
        {
            
            StringBuilder sb = new StringBuilder(1000);
            sb.AppendLine("This is where you could Persist the new Note");
            sb.AppendFormat("Date {0}{1}{2}",
                note.DateCreated,
                Environment.NewLine,
                note.Data);
            //MessageBox.Show(note.DateCreated.ToString());
            String strConnection = "Data Source=.\\SqlExpress;Initial Catalog=requirement;Integrated Security=True";
            SqlConnection Connection = new SqlConnection(strConnection);
            Connection.Open();
            string lenh = "insert into sticky (idProject,date,stickyContent) values (1,'" + note.DateCreated.ToString() + "','" + note.Data + "')";
            SqlCommand sqlcmd = new SqlCommand(lenh, Connection);
            sqlcmd.CommandType = CommandType.Text;
            sqlcmd.ExecuteNonQuery();
            Connection.Close();            
            
            return sb.ToString();
        }

        private void lstPeople_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            notesAdornerDecorator.DisplayNotes = (lstPeople.SelectedItem as Person).Notes;
        }





        #endregion
    }
}
